<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <el-col>操作</el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree :data="list" :props="defaultProps" :default-expand-all="true">
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartment(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepartment(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="delDepartment(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 新增时的对话框 -->
      <add-department
        ref="addDepart"
        :show-dialog="dialogVisible"
        :cur-id="curId"
        @close-dialog="closeDialog"
        @reset-data="resetData"
      />
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/department'
import transTree from '@/utils/transTree'
import addDepartment from './components/add-department.vue'
export default {
  components: {
    addDepartment
  },
  data() {
    return {
      // 树形数组
      list: [{
        name: '财务部',
        manager: '张三',
        children: [
          {
            name: '财务核算部',
            manager: '李四'
          },
          {
            name: '税务核算部',
            manager: '王五'
          }
        ]
      }],
      // 修改默认配置
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      dialogVisible: false,
      curId: ''
    }
  },
  mounted() {
    this.fetchDepartments()
  },
  methods: {
    async fetchDepartments() {
      const { depts } = await getDepartments()
      this.list = transTree(depts)
    },
    // 弹出对话框
    addDepartment(id) {
      this.dialogVisible = true
      this.curId = id
    },
    // 关闭对话框 - 供子元素调用
    closeDialog() {
      this.dialogVisible = false
    },
    // 供子元素使用 - 刷新列表 清空 curId
    resetData() {
      this.fetchDepartments()
      this.curId = ''
    },
    // 删除操作
    delDepartment(id) {
      this.$confirm('你确认要进行删除么?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 点击确定
        // 1. 调用接口 - 删除数据
        await delDepartments(id)
        // 2. 提示用户
        this.$message.success('删除成功')
        // 3. 刷新列表
        this.fetchDepartments()
      })
    },
    // 新增模块
    editDepartment(id) {
      this.dialogVisible = true
      this.$refs.addDepart.getDetail(id)
    }
  }
}
</script>
